package org.dallasrowing.logbook.ui.controller;

import org.dallasrowing.logbook.ui.view.LogBookWindow;
import org.dallasrowing.logbook.ui.view.NewRowerWindow;
import org.dallasrowing.model.Rower;
import org.dallasrowing.model.dao.DAOException;
import org.dallasrowing.model.dao.RowersDAO;

public class RowerMissingControllerImpl implements RowerMissingController {

	private RowersDAO rowersDAO;
	private LogBookWindow mainWindow;
	private NewRowerWindow newRowerWindow;
	private NewLogEntryController newLogEntryController;

	public RowerMissingControllerImpl(LogBookWindow mainWindow,
			NewLogEntryController newLogEntryController, RowersDAO rowersDAO) {
		this.rowersDAO = rowersDAO;
		this.mainWindow = mainWindow;
		this.newLogEntryController = newLogEntryController;
	}

	public void newNewRowerWindow(NewRowerWindow newRowerWindow) {
		this.newRowerWindow = newRowerWindow;
	}

	public void openNewRowerWindow() {
		newRowerWindow.addCreateRowerListener(new Runnable() {
			public void run() {
				createNewRower();
			}
		});
	}

	protected void createNewRower() {
		Rower rower = new Rower(0,
				                newRowerWindow.getName().trim(),
				                newRowerWindow.getEmail().trim(),
				                newRowerWindow.getPhone().trim(),
				                newRowerWindow.getLevel(),
				                newRowerWindow.isMember(), true );
		
		try {
			rowersDAO.createNewRower(rower);
		} catch (DAOException e) {
			mainWindow.showError(e);
		}
		
		newLogEntryController.refreshRowers();
		newRowerWindow.close();
	}
}
